// 获取当前HOST
if (typeof http_url === 'undefined') {
    var http_urla = document.scripts;
    var http_urlb = http_urla[http_urla.length-1].src;
    var http_url = http_urlb.substring(0,http_urlb.lastIndexOf("/"));
    var http_url = http_url.substring(0,http_url.lastIndexOf("/")+1);
}

// 皮肤json数组
var skins = [
    {"skin_name":"当前蔚蓝(下个黑白)", "skin_id":1, "skin_icon":"◨", "skin_css":http_link+"admin/css/blue.css"},
    {"skin_name":"当前黑白(下个简白)", "skin_id":2, "skin_icon":"◨", "skin_css":http_link+"admin/css/black.css"},
    {"skin_name":"当前简白(下个炫黑)", "skin_id":3, "skin_icon":"◫", "skin_css":http_link+"admin/css/white.css"},
    {"skin_name":"当前炫黑(下个蔚蓝)", "skin_id":4, "skin_icon":"◫", "skin_css":http_link+"admin/css/grey.css"}
];

/**
 * 默认皮肤初始化
 */
if (typeof localStorage.historyIndexSearchItems != 'undefined') {
    var skinId = localStorage.historyIndexSearchItems;
}else{
    var skinId = 1; // 默认皮肤
}
setHistoryItems(skinId);
getHistoryInfo();

/**
 * 设置历史记录
 */
function setHistoryItems(keyword) {
    localStorage.removeItem('historyIndexSearchItems');
    //过滤一个结果的空记录添加，过滤空搜索  默认保存10条记录，自己可修改
    //keyword = keyword.replace("/^\s+|\s+$/g","");
    let { historyIndexSearchItems } = localStorage;
    if (historyIndexSearchItems === undefined) {
        localStorage.historyIndexSearchItems = keyword;
    } else {
        const onlyItem = historyIndexSearchItems.split('|').filter(e => e != keyword);
        if (onlyItem.length > 0){
            historyIndexSearchItems = keyword + '|' + onlyItem.slice(0,9).join('|');
        }
        localStorage.historyIndexSearchItems = historyIndexSearchItems;
    }
}

/**
 * 清除历史记录
 */
function clearHistory() {
    localStorage.removeItem('historyIndexSearchItems');
    window.location.reload();
}

//动态加载文件方法
function loadjscssfile(filename, filetype) {
    if (filetype == "js") {
        var fileref = document.createElement('script')
        fileref.setAttribute("type", "text/javascript")
        fileref.setAttribute("src", filename)
    } else if (filetype == "css") {
        var fileref = document.createElement("link")
        fileref.setAttribute("rel", "stylesheet")
        fileref.setAttribute("type", "text/css")
        fileref.setAttribute("href", filename)
    }
    if (typeof fileref != "undefined"){
        document.getElementsByTagName("head")[0].appendChild(fileref)
    }
}

/**
 * 获取搜索历史记录进行充值皮肤
 */
function getHistoryInfo(){
    var skinInfo = localStorage.historyIndexSearchItems;
    if(skinInfo != undefined){
        for(let i in skins){
            if(skinInfo == skins[i].skin_id){
                $('.right_top_btn4').text(skins[i].skin_icon);
                $('.right_top_btn4').attr('title', skins[i].skin_name);
                loadjscssfile(skins[i].skin_css,'css');
            }
        }
    }
}

// 手动皮肤切换
$('.right_top_btn4').click(function(){
    if (typeof localStorage.historyIndexSearchItems != 'undefined') {
        skinId = localStorage.historyIndexSearchItems;
    }
    if(skins.length > skinId){
        skinId = parseInt(parseInt(skinId) + 1);
    }else{
        skinId = 1;
    }
    setHistoryItems(skinId);
    getHistoryInfo();
    setTimeout(function(){
        window.location.href='';
    },1000);
});